home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap05 / AltWind / AltWind.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.1 KB  |  101 lines

  1. /*-----------------------------------------------
  2.    ALTWIND.C -- Alternate and Winding Fill Modes
  3.                 (c) Charles Petzold, 1998
  4.   -----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName[] = TEXT ("AltWind") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.      
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.      
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("Alternate and Winding Fill Modes"),
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.      
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.      
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  54. {
  55.      static POINT aptFigure [10] = { 10,70, 50,70, 50,10, 90,10, 90,50,
  56.                                      30,50, 30,90, 70,90, 70,30, 10,30 };
  57.      static int   cxClient, cyClient ;
  58.      HDC          hdc ;
  59.      int          i ;
  60.      PAINTSTRUCT  ps ;
  61.      POINT        apt[10] ;
  62.      
  63.      switch (message)
  64.      {
  65.      case WM_SIZE:
  66.           cxClient = LOWORD (lParam) ;
  67.           cyClient = HIWORD (lParam) ;
  68.           return 0 ;
  69.  
  70.      case WM_PAINT:
  71.           hdc = BeginPaint (hwnd, &ps) ;
  72.  
  73.           SelectObject (hdc, GetStockObject (GRAY_BRUSH)) ;
  74.  
  75.           for (i = 0 ; i < 10 ; i++)
  76.           {
  77.                apt[i].x = cxClient * aptFigure[i].x / 200 ;
  78.                apt[i].y = cyClient * aptFigure[i].y / 100 ;
  79.           }
  80.  
  81.           SetPolyFillMode (hdc, ALTERNATE) ;
  82.           Polygon (hdc, apt, 10) ;
  83.  
  84.           for (i = 0 ; i < 10 ; i++)
  85.           {
  86.                apt[i].x += cxClient / 2 ;
  87.           }
  88.  
  89.           SetPolyFillMode (hdc, WINDING) ;
  90.           Polygon (hdc, apt, 10) ;
  91.           
  92.           EndPaint (hwnd, &ps) ;
  93.           return 0 ;
  94.           
  95.      case WM_DESTROY:
  96.           PostQuitMessage (0) ;
  97.           return 0 ;
  98.      }
  99.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  100. }
  101.